package pathways;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import pathways.Database.Implementation.CDBConnection;
import pathways.Database.Implementation.CDatabase;
import pathways.Database.Specialized.Visual.CDBSVSearchReaction;
import pathways.Database.Specialized.Visual.CDBSVSynonyms;
import pathways.Database.Specialized.Visual.CEditCompound;
import pathways.Database.Specialized.Visual.CSearchCompound;
import pathways.util.Debug.CDebug;

public class CEditReaction extends JFrame{
  JLabel lblSubstrate = new JLabel();
  JComboBox cboSubstrates = new JComboBox();
  JButton cmdSubstrateSearch = new JButton();
  JComboBox cboEnzyme = new JComboBox();
  JLabel lblEnzyme = new JLabel();
  JButton cmdSearchEnzyme = new JButton();
  JComboBox cboProduct = new JComboBox();
  JLabel lblProduct = new JLabel();
  JButton cmdSearchProduct = new JButton();
  JLabel lblAdditionalSubstrates = new JLabel();
  JScrollPane scrollPaneAdditionalSubs = new JScrollPane();
  JList lstAdditionalSubs = new JList();
  JButton cmdSearchAdditionalSub = new JButton();
  JButton cmdRemoveAdditionalSub = new JButton();
  JButton cmdSearchAdditionalProd = new JButton();
  JLabel lblAdditionalProducts = new JLabel();
  JButton cmdRemoveAdditionalProd = new JButton();
  JScrollPane scrollPaneAdditionalProds = new JScrollPane();
  JScrollPane scrollPaneCoProds = new JScrollPane();
  JLabel lblCoProd = new JLabel();
  JButton cmdRemoveCoProd = new JButton();
  JButton cmdRemoveCoSub = new JButton();
  JList lstCoSub = new JList();
  JLabel lblCoSubstrates = new JLabel();
  JButton cmdSearchCoProd = new JButton();
  JButton cmdSearchCoSub = new JButton();
  JScrollPane scrollPaneCoSubs = new JScrollPane();
  JButton cmdSearchRxn = new JButton();
  JButton cmdSave = new JButton();
  JButton cmdClear = new JButton();
  JList lstAdditionalProds = new JList();
  JList lstCoProd = new JList();
  
  private static void setUpDebug()
  {
    
    CDebug.addCategory("pathways.Database.Implementation.CDBConnection");
    CDebug.addCategory("pathways.Database.Implementation.CDBUpdate");
    //CDebug.addCategory("pathways.Database.Specialized.Visual.CDBSVSearchBySubstrate");
    //CDebug.addCategory("pathways.Database.Specialized.Visual.CProductListModel");  
    //CDebug.addCategory("pathways.Database.Specialized.Visual.CEnzymeListModel");
    //CDebug.addCategory("pathways.CSubstrateMouseListener");
    CDebug.addCategory("pathways.CCompound");
    CDebug.addCategory("pathways.CReaction");
    
    CDebug.addCategory("pathways.Database.Specialized.Visual.CSearchCompound");
    CDebug.addCategory("pathways.Database.Specialized.Visual.CMyListModel");
    CDebug.addCategory("pathways.CEditReaction");
  }
  
  public static void main(String args[])
  { 
    setUpDebug();
      
    CDBConnection conn = new CDBConnection();
    
    try {
        conn.setClass("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        conn.setConnect("jdbc:microsoft:sqlserver://127.0.0.1:1433;user=sa;password=sa;databasename=pathways;SelectMethod=cursor;");
    } catch (Exception excep) 
    { 
      //System.out.println("Unable to establish connection to database");  
    }
    
    CDatabase db = new CDatabase(conn);   
    
    CReaction rxn = new CReaction();
    
    rxn.read(25,db);
      
    new CEditReaction(rxn,db).show();  
  }
  
  private void jbInit() throws Exception {
    lblSubstrate.setHorizontalAlignment(SwingConstants.RIGHT);
    lblSubstrate.setText("Substrate:");
    lblSubstrate.setBounds(new Rectangle(5, 8, 61, 14));
    this.getContentPane().setLayout(null);
    cboSubstrates.setBounds(new Rectangle(70, 7, 172, 19));
    cmdSubstrateSearch.setText("Search");
    cmdSubstrateSearch.setBounds(new Rectangle(250, 7, 83, 23));
    cboEnzyme.setBounds(new Rectangle(69, 45, 172, 19));
    lblEnzyme.setBounds(new Rectangle(3, 46, 63, 14));
    lblEnzyme.setHorizontalAlignment(SwingConstants.RIGHT);
    lblEnzyme.setText("Enzyme:");
    cmdSearchEnzyme.setBounds(new Rectangle(249, 45, 83, 23));
    cmdSearchEnzyme.setText("Search");
    cboProduct.setBounds(new Rectangle(70, 81, 172, 19));
    lblProduct.setHorizontalAlignment(SwingConstants.RIGHT);
    lblProduct.setText("Product:");
    lblProduct.setBounds(new Rectangle(3, 82, 63, 14));
    cmdSearchProduct.setText("Search");
    cmdSearchProduct.setBounds(new Rectangle(250, 81, 82, 23));
    lblAdditionalSubstrates.setText("Additional Substrates:");
    lblAdditionalSubstrates.setBounds(new Rectangle(32, 119, 126, 21));
    scrollPaneAdditionalSubs.setBounds(new Rectangle(12, 143, 172, 163));
    cmdSearchAdditionalSub.setBounds(new Rectangle(190, 173, 82, 23));
    cmdSearchAdditionalSub.setText("Search");
    cmdRemoveAdditionalSub.setBounds(new Rectangle(191, 203, 81, 23));
    cmdRemoveAdditionalSub.setText("Remove");
    cmdSearchAdditionalProd.setText("Search");
    cmdSearchAdditionalProd.setBounds(new Rectangle(192, 364, 82, 23));
    lblAdditionalProducts.setBounds(new Rectangle(34, 310, 126, 21));
    lblAdditionalProducts.setText("Additional Products:");
    cmdRemoveAdditionalProd.setText("Remove");
    cmdRemoveAdditionalProd.setBounds(new Rectangle(193, 394, 81, 23));
    scrollPaneAdditionalProds.setBounds(new Rectangle(14, 332, 172, 163));
    scrollPaneCoProds.setBounds(new Rectangle(288, 332, 172, 163));
    lblCoProd.setBounds(new Rectangle(308, 308, 126, 21));
    lblCoProd.setText("Co-Products:");
    cmdRemoveCoProd.setText("Remove");
    cmdRemoveCoProd.setBounds(new Rectangle(467, 392, 81, 23));
    cmdRemoveCoSub.setBounds(new Rectangle(465, 201, 81, 23));
    cmdRemoveCoSub.setText("Remove");
    lblCoSubstrates.setText("Co-Substrates:");
    lblCoSubstrates.setBounds(new Rectangle(306, 117, 126, 21));
    cmdSearchCoProd.setText("Search");
    cmdSearchCoProd.setBounds(new Rectangle(466, 362, 82, 23));
    cmdSearchCoSub.setBounds(new Rectangle(464, 171, 82, 23));
    cmdSearchCoSub.setText("Search");
    scrollPaneCoSubs.setBounds(new Rectangle(287, 138, 172, 163));
    cmdSearchRxn.setText("Search Reaction");
    cmdSearchRxn.setBounds(new Rectangle(233, 508, 143, 27));
    cmdSearchRxn.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdSearchRxn_actionPerformed(e);
      }
    });
    cmdSave.setText("Save");
    cmdSave.setBounds(new Rectangle(158, 509, 64, 26));
    cmdClear.setBounds(new Rectangle(394, 509, 64, 26));
    cmdClear.setText("Clear");
    lblInhibitors.setText("Inhibitors:");
    lblInhibitors.setBounds(new Rectangle(581, 309, 126, 21));
    scrollPaneInhibitors.setBounds(new Rectangle(561, 331, 124, 163));
    cmdSearchInhibitors.setText("Search");
    cmdSearchInhibitors.setBounds(new Rectangle(689, 358, 82, 23));
    cmdSearchInhibitors.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdSearchInhibitors_actionPerformed(e);
      }
    });
    cmdRemoveInhibitors.setText("Remove");
    cmdRemoveInhibitors.setBounds(new Rectangle(690, 388, 81, 23));
    cmdRemoveInhibitors.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdRemoveInhibitors_actionPerformed(e);
      }
    });
    cmdSynonyms.setText("Synonynms");
    cmdSynonyms.setBounds(new Rectangle(608, 62, 139, 25));
    cmdSynonyms.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdSynonyms_actionPerformed(e);
      }
    });
    cmdEditCmpd.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        cmdEditCmpd_actionPerformed(e);
      }
    });
    cmdEditCmpd.setBounds(new Rectangle(608, 29, 139, 25));
    cmdEditCmpd.setText("Edit Compound");
    this.getContentPane().add(cboSubstrates, null);
    this.getContentPane().add(cboEnzyme, null);
    this.getContentPane().add(cmdSubstrateSearch, null);
    this.getContentPane().add(cmdSearchEnzyme, null);
    this.getContentPane().add(cboProduct, null);
    this.getContentPane().add(cmdSearchProduct, null);
    this.getContentPane().add(lblProduct, null);
    this.getContentPane().add(lblSubstrate, null);
    this.getContentPane().add(lblEnzyme, null);
    this.getContentPane().add(cmdRemoveAdditionalSub, null);
    this.getContentPane().add(cmdSearchAdditionalSub, null);
    this.getContentPane().add(scrollPaneAdditionalSubs, null);
    this.getContentPane().add(lblAdditionalSubstrates, null);
    this.getContentPane().add(cmdSearchAdditionalProd, null);
    this.getContentPane().add(lblAdditionalProducts, null);
    this.getContentPane().add(cmdRemoveAdditionalProd, null);
    this.getContentPane().add(scrollPaneAdditionalProds, null);
    this.getContentPane().add(cmdSearchRxn, null);
    this.getContentPane().add(cmdSave, null);
    this.getContentPane().add(cmdClear, null);
    this.getContentPane().add(cmdRemoveCoSub, null);
    this.getContentPane().add(cmdSearchCoSub, null);
    this.getContentPane().add(lblCoSubstrates, null);
    this.getContentPane().add(scrollPaneCoSubs, null);
    this.getContentPane().add(scrollPaneCoProds, null);
    this.getContentPane().add(lblCoProd, null);
    this.getContentPane().add(cmdRemoveCoProd, null);
    this.getContentPane().add(cmdSearchCoProd, null);
    this.getContentPane().add(cmdRemoveInhibitors, null);
    this.getContentPane().add(lblInhibitors, null);
    this.getContentPane().add(scrollPaneInhibitors, null);
    this.getContentPane().add(cmdSearchInhibitors, null);
    this.getContentPane().add(cmdSynonyms, null);
    this.getContentPane().add(cmdEditCmpd, null);
    scrollPaneInhibitors.getViewport().add(lstInhibitors, null);
    scrollPaneCoProds.getViewport().add(lstCoProd, null);
    scrollPaneCoSubs.getViewport().add(lstCoSub, null);
    scrollPaneAdditionalProds.getViewport().add(lstAdditionalProds, null);
    scrollPaneAdditionalSubs.getViewport().add(lstAdditionalSubs, null);
  }

  public CEditReaction(CReaction rxn, CDatabase database) {
    
    try {
      jbInit();      
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    
    setSize(800,600);
    
    lstmdlAdditionalSubs = new CMyListModel();
    lstmdlAdditionalProds = new CMyListModel();
    lstmdlCoSubs = new CMyListModel();
    lstmdlCoProds = new CMyListModel();
    
    this.lstAdditionalSubs.setModel(this.lstmdlAdditionalSubs);
    this.lstAdditionalProds.setModel(this.lstmdlAdditionalProds);
    this.lstCoSub.setModel(this.lstmdlCoSubs);
    this.lstCoProd.setModel(this.lstmdlCoProds);
    
    this.cmdSubstrateSearch.addActionListener(new searchSubstrateListener(this));
    this.cmdSearchEnzyme.addActionListener(new searchEnzymeListener(this));
    this.cmdSearchProduct.addActionListener(new searchProductListener(this));
    this.cmdSearchAdditionalSub.addActionListener(new searchAdditionalSubsListener(this));
    this.cmdSearchAdditionalProd.addActionListener(new searchAdditionalProdsListener(this));
    this.cmdSearchCoSub.addActionListener(new searchCoSubsListener(this));
    this.cmdSearchCoProd.addActionListener(new searchCoProdsListener(this));
    
    this.cmdRemoveAdditionalSub.addActionListener(new removeAdditionalSubListener(this));
    this.cmdRemoveAdditionalProd.addActionListener(new removeAdditionalProdListener(this));
    this.cmdRemoveCoSub.addActionListener(new removeCoSubListener(this));
    this.cmdRemoveCoProd.addActionListener(new removeCoProdListener(this));
    
    this.cmdClear.addActionListener(new clearScreenListener(this));
    this.cmdSave.addActionListener(new saveListener(this));
    
    reaction = rxn;
    db = database;
    
    if (reaction != null)
       loadReaction(reaction);
    else
      reaction = new CReaction();       
  }
  
  private void clearScreen()
  {
    Vector empty = new Vector();
    
    reaction = new CReaction();
    
    CMyComboModel c = new CMyComboModel(empty);
    this.cboSubstrates.setModel(c);
    this.cboEnzyme.setModel(c);
    this.cboProduct.setModel(c);
    
    CMyListModel model = new CMyListModel(empty);
    
    this.lstAdditionalSubs.setModel(model);
    this.lstAdditionalProds.setModel(model);
    this.lstCoSub.setModel(model);
    this.lstCoProd.setModel(model);
    this.lstInhibitors.setModel(model);
  }
  
  private boolean loadReaction(CReaction rxn)
  {
    // Pre: rxn.read() has already been successfully called
    
    // show substrate
    showSubstrate(rxn);
    
    // show enzyme
    showEnzyme(rxn);
    
    // show product
    showProduct(rxn);
    
    // show additional subs
    showAdditionalSubs(rxn);
    
    // show additional prods
    showAdditionalProds(rxn);
        
    // show co-Subs
    showCoSubstrates(rxn);
    
    // show co-Prods
    showCoProducts(rxn);
    
    showInhibitors(rxn);
    
    return true; 
  }
  
  private boolean isOK()
  {
    // check 
    // subs selected
    // enzyme selected
    // prod selected
    
    CSubstrate sub = reaction.getSubstrate();
    CEnzyme enzyme = reaction.getEnzyme();
    CSubstrate prod = reaction.getProduct();
    
    if (sub.getID() == 0)
    {
      JOptionPane.showMessageDialog(this,"Please select a substrate.","Error",JOptionPane.ERROR_MESSAGE);
      return false;  
    }
    
    if (enzyme.getID() == 0)
    {
      JOptionPane.showMessageDialog(this,"Please select an enzyme.","Error",JOptionPane.ERROR_MESSAGE);
      return false;  
    }
    
    if (prod.getID() == 0)
    {
      JOptionPane.showMessageDialog(this,"Please select a product.","Error",JOptionPane.ERROR_MESSAGE);
      return false;  
    }
    
    return true;  
  }
  
  private void saveReaction()
  {
    if (isOK())
    {
      try {
          db.getConnection().beginTrans();
      } catch (SQLException e) {
        CDebug.println(this, "saveReaction() - while starting transaction Exception: " + e.toString());
        JOptionPane.showMessageDialog(this,"Unable to save reaction.  Could not start a transaction.","Error",JOptionPane.ERROR_MESSAGE);
        return;  
      }
      
      if (reaction.save(db))
      {
         try {
             db.getConnection().commitTrans();
             System.out.println("Trans committed");
             JOptionPane.showMessageDialog(this,"Saved successfully.","Saved",JOptionPane.INFORMATION_MESSAGE);
         } catch (SQLException e) {
           CDebug.println(this, "saveReaction() - while commiting transaction Exception: " + e.toString());
           JOptionPane.showMessageDialog(this,"Unable to save reaction. Unable to commit transaction.","Error",JOptionPane.ERROR_MESSAGE); 
         }
      }
      else
      {
        try {
            db.getConnection().rollbackTrans();
            System.out.println("Trans rolledback.");
        } catch(SQLException e) {
          CDebug.println(this, "saveReaction() - while rollingback transaction Exception: " + e.toString());
        }
        
        JOptionPane.showMessageDialog(this,"Unable to save reaction.","Error",JOptionPane.ERROR_MESSAGE);
      }
    }  
  }
  
  private void showSubstrate(CReaction rxn)
  {
    // get all names for the substrate
    CMyComboModel c = new CMyComboModel(rxn.getSubstrate().getAllNames(db));
    this.cboSubstrates.setModel(c); 
  }
  
  private void showProduct(CReaction rxn)
  {
    CMyComboModel c = new CMyComboModel(rxn.getProduct().getAllNames(db));
    this.cboProduct.setModel(c); 
  }
  
  private void showEnzyme(CReaction rxn)
  {
    CMyComboModel c = new CMyComboModel(rxn.getEnzyme().getAllNames(db));
    this.cboEnzyme.setModel(c); 
  }
  
  private void showAdditionalSubs(CReaction rxn)
  {
    this.lstmdlAdditionalSubs = new CMyListModel(rxn.getVisibleAdditionalSubs());
    this.lstAdditionalSubs.setModel(this.lstmdlAdditionalSubs);
  }
  
  private void showAdditionalProds(CReaction rxn)
  {
    this.lstmdlAdditionalProds = new CMyListModel(rxn.getVisibleAdditionalProds());
    this.lstAdditionalProds.setModel(this.lstmdlAdditionalProds);
  }
  
  private void showCoSubstrates(CReaction rxn)
  {
    this.lstmdlCoSubs = new CMyListModel(rxn.getVisibleCoSubstrates());
    this.lstCoSub.setModel(this.lstmdlCoSubs);
  }
  
  private void showCoProducts(CReaction rxn)
  {
    this.lstmdlCoProds = new CMyListModel(rxn.getVisibleCoProducts());
    this.lstCoProd.setModel(this.lstmdlCoProds);
  }

  private void showInhibitors(CReaction rxn)
  {
    this.lstmdlInhibitors = new CMyListModel(rxn.getVisibleInhibitors());
    this.lstInhibitors.setModel(this.lstmdlInhibitors);
  }
  
  public void buttonClicked(ActionEvent e, int id)
  {
    switch(id)
    {
      case 0:
           searchSubstrate();
           break;
      case 1:
           searchEnzyme();
           break;
      case 2:
           searchProduct();
           break;
      case 3:
           searchAdditionalSub();
           break;
      case 4:
           searchAdditionalProd();
           break;
      case 5:
           searchCoSub();
           break;
      case 6:
           searchCoProd();
           break;
      case 7:
           removeAdditionalSub();
           break;
      case 8:
           removeAdditionalProd();
           break;
      case 9:
           removeCoSub();
           break;
      case 10:
           removeCoProd();
           break;
      case 11:
           clearScreen();
           break;
      case 12:
           saveReaction();
           break;
    }
  }
  
  private void removeAdditionalSub()
  {
    int index = this.lstAdditionalSubs.getSelectedIndex();
    
    if (index >= 0)
    {
      CCoCompound c = (CCoCompound)this.lstmdlAdditionalSubs.getCoCompound(index);
      
      reaction.removeAdditionalSub(c);
      
      showAdditionalSubs(reaction);
    }
  }
  
  private void removeAdditionalProd()
  {
    int index = this.lstAdditionalProds.getSelectedIndex();
    
    if (index >= 0)
    {
      CCoCompound c = (CCoCompound)this.lstmdlAdditionalProds.getCoCompound(index);
    
      reaction.removeAdditionalProd(c);
    
      showAdditionalProds(reaction);
    }
  }
  
  private void removeCoSub()
  {
    int index = this.lstCoSub.getSelectedIndex();
    
    if (index >= 0)
    {
      CCoCompound c = (CCoCompound)this.lstmdlCoSubs.getCoCompound(index);
    
      reaction.removeCoSubstrate(c);
    
      showCoSubstrates(reaction);
    }
  }
  
  private void removeCoProd()
  {
    int index = this.lstCoProd.getSelectedIndex();
    
    if (index >= 0)
    {
      CCoCompound c = (CCoCompound)this.lstmdlCoProds.getCoCompound(index);
    
      reaction.removeCoProduct(c);
    
      showCoProducts(reaction);
    }
  }
  
  private void removeInhibitor()
  {
    int index = this.lstInhibitors.getSelectedIndex();
    
    if (index >= 0)
    {
      CCoCompound c = (CCoCompound)this.lstmdlInhibitors.getCoCompound(index);
    
      reaction.removeInhibitor(c);
    
      showInhibitors(reaction);
    }
  }
  
  private CCompound searchCompound()
  {
    CSearchCompound c = new CSearchCompound(true,this.getFrames()[0],db,CSearchCompound.FILTER_NONE);
    
    c.show();
    
    return c.getSelectedCompound();
  }
  
  public void searchSubstrate()
  {
    CCompound c = searchCompound();
    
    if (c != null)
    {
      CSubstrate s = new CSubstrate(c.getName(),c.getID());
      
      reaction.rawSetSubstrate(s);
      
      showSubstrate(reaction);
    }
  }
  
  public void searchProduct()
  {
    CCompound c = searchCompound();
    
    if (c != null)
    {
      CSubstrate s = new CSubstrate(c.getName(),c.getID());
      
      reaction.rawSetProduct(s);
      
      showProduct(reaction);
    }
  }
  
  public void searchEnzyme()
  {
    CCompound c = searchCompound();
    
    if (c != null)
    {
      CEnzyme e = new CEnzyme(c.getName(),c.getID());
      
      reaction.rawSetEnzyme(e);
      
      showEnzyme(reaction);
    }
  }
  
  public void searchAdditionalSub()
  {
    CCompound c = searchCompound();
    
    if (c != null)
    {
      reaction.addAdditionalSub(new CCoCompound(c,CCoCompound.NEW));
      showAdditionalSubs(reaction);
    }
  }
  
  public void searchAdditionalProd()
  {
    CCompound c = searchCompound();
    
    if (c != null)
    {
      reaction.addAdditionalProd(new CCoCompound(c,CCoCompound.NEW));
      showAdditionalProds(reaction);
    }
  }

  public void searchInhibitor()
  {
    CCompound c = searchCompound();
    
    if (c != null)
    {
      reaction.addInhibitor(new CCoCompound(c,CCoCompound.NEW));
      showInhibitors(reaction);
    }
  }
  
  public void searchCoSub()
  {
    CCompound c = searchCompound();
    
    if (c != null)
    {
      reaction.addCoSubstrate(new CCoCompound(c,CCoCompound.NEW));
      showCoSubstrates(reaction);
    }
  }
  
  public void searchCoProd()
  {
    CCompound c = searchCompound();
    
    if (c != null)
    {
      reaction.addCoProduct(new CCoCompound(c,CCoCompound.NEW));
      showCoProducts(reaction);
    }
  }
  
  private class searchSubstrateListener implements ActionListener
  {
    public searchSubstrateListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,0);
    }
    
    private CEditReaction editRxn;
  }
  
  private class searchEnzymeListener implements ActionListener
  {
    public searchEnzymeListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,1);
    }
    
    private CEditReaction editRxn;
  }
  
  private class searchProductListener implements ActionListener
  {
    public searchProductListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,2);
    }
    
    private CEditReaction editRxn;
  }

  private class searchAdditionalSubsListener implements ActionListener
  {
    public searchAdditionalSubsListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,3);
    }
    
    private CEditReaction editRxn;
  }

  private class searchAdditionalProdsListener implements ActionListener
  {
    public searchAdditionalProdsListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,4);
    }
    
    private CEditReaction editRxn;
  }
  
  private class searchCoSubsListener implements ActionListener
  {
    public searchCoSubsListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,5);
    }
    
    private CEditReaction editRxn;
  }

  private class searchCoProdsListener implements ActionListener
  {
    public searchCoProdsListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,6);
    }
    
    private CEditReaction editRxn;
  }
  
  private class removeAdditionalSubListener implements ActionListener
  {
    public removeAdditionalSubListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,7);
    }
    
    private CEditReaction editRxn;
  }
  
  private class removeAdditionalProdListener implements ActionListener
  {
    public removeAdditionalProdListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,8);
    }
    
    private CEditReaction editRxn;
  }

  private class removeCoSubListener implements ActionListener
  {
    public removeCoSubListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,9);
    }
    
    private CEditReaction editRxn;
  }
  
  private class removeCoProdListener implements ActionListener
  {
    public removeCoProdListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,10);
    }
    
    private CEditReaction editRxn;
  }

  private class clearScreenListener implements ActionListener
  {
    public clearScreenListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,11);
    }
    
    private CEditReaction editRxn;
  }

  private class saveListener implements ActionListener
  {
    public saveListener(CEditReaction r)
    {
      editRxn = r;  
    }
    
    public void actionPerformed(ActionEvent e)
    {
      editRxn.buttonClicked(e,12);
    }
    
    private CEditReaction editRxn;
  }
  
  private CMyListModel lstmdlAdditionalSubs;
  private CMyListModel lstmdlAdditionalProds;
  private CMyListModel lstmdlCoSubs;
  private CMyListModel lstmdlCoProds;
  private CMyListModel lstmdlInhibitors;
  private CReaction reaction;
  private CDatabase db;
  JLabel lblInhibitors = new JLabel();
  JScrollPane scrollPaneInhibitors = new JScrollPane();
  JButton cmdSearchInhibitors = new JButton();
  JButton cmdRemoveInhibitors = new JButton();
  JList lstInhibitors = new JList();
  JButton cmdSynonyms = new JButton();
  JButton cmdEditCmpd = new JButton();

  void cmdSearchRxn_actionPerformed(ActionEvent e)
  {
    Rectangle dontcare = new Rectangle(0,0,100,100);
    
    CDBSVSearchReaction c = new CDBSVSearchReaction(db,this.getFrames()[0],dontcare,dontcare,0,2);
    c.show();
    
    CReaction rxn = c.getReaction();
    
    if (rxn != null)
    {
      reaction = rxn;
      
      reaction.read(reaction.getID(),db);
      
      loadReaction(reaction); 
    }
  }

  void cmdSearchInhibitors_actionPerformed(ActionEvent e)
  {
    searchInhibitor();
  }

  void cmdRemoveInhibitors_actionPerformed(ActionEvent e)
  {
    removeInhibitor();
  }

  void cmdSynonyms_actionPerformed(ActionEvent e)
  {
    CDBSVSynonyms syn = new CDBSVSynonyms(db);
    
    syn.show();
  }
  void cmdEditCmpd_actionPerformed(ActionEvent e)
  {
    CCompound cmpd = searchCompound();
    
    if (cmpd != null)
    {
      CEditCompound editCmpd = new CEditCompound(cmpd,db,this);
      editCmpd.show();  
    }
    
  }
}

class CMyListModel extends AbstractListModel
{
  public CMyListModel()
  {
    data = new Vector(); 
  }
  
  public CMyListModel(Vector v)
  {
    data = v; 
  }
  
  public void setData(Vector v)
  {
    data = v;  
  }
  
  public Vector getData()
  {
    return data;  
  }
  
  public Object getElementAt(int index)
  {
    return ((CCoCompound)data.get(index)).getCompound().getName();
  }
  
  public int getSize()
  {      
    return data.size();
  }
  
  public CCoCompound getCoCompound(int index)
  {
    return (CCoCompound)data.get(index);  
  }
  
  private Vector data;
}

class CMyComboModel extends AbstractListModel implements ComboBoxModel
{
  public CMyComboModel(Vector v)
  {
    data = v;  
  }
  
  public Object getElementAt(int index)
  {
    return data.get(index);
  }
  
  public int getSize()
  {
     return data.size();
  }
  
  public Object getSelectedItem()
  {
    return selectedItem;
  }
  
  public void setSelectedItem(Object item)
  {
    selectedItem = item;
  }
  
  
  private Object selectedItem; 
  private Vector data; 
}